require "BagTableLayer"
require "HeroEquipLayer"

ZBHCLayer = {}
ZBHCLayerFunc = {}
ZBHCLayerFunc.__index = ZBHCLayerFunc
setmetatable(ZBHCLayer, ZBHCLayerFunc)
ZBHCLayer.NodeID = {Menu1=1001, Menu2=1002, TableBK=1003, QiangHuaButton=1004, Item1=2001, Item2=2002}

ZBHCLayerFunc.getNode = function()
  local l = {}
  setmetatable(l, ZBHCLayerFunc)
  l.node = CCBReader:load("ZhuangBeiLayer1.ccbi")
  ZBHCLayer.instance = l
  l:init()
  return l
end
ZBHCLayerFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(ZBHCLayer:getNode().node)
  print("ZBHCLayer.getScene", sc)
  return sc
end

ZBHCLayerFunc.init = function(self)
  local menu = self.node:getChildByTag(ZBHCLayer.NodeID.Menu1)
  self.radioMenu1 = RadioMenu:create(menu, "menuItem1Changed", self)
  menu = self.node:getChildByTag(ZBHCLayer.NodeID.Menu2)
  self.radioMenu2 = RadioMenu:create(menu, "menuItem2Changed", self)

  --[[
  print("ZBHCLayerFunc.init node", self.node, menu)
  self.item1 = menu:getChildByTag(ZBHCLayer.NodeID.Item1)
  setObjectExtTable(self.item1, self);
  self.item2 = menu:getChildByTag(ZBHCLayer.NodeID.Item2)
  setObjectExtTable(self.item2, self);
  print("item", self.item1, self.item2)
  ]]
  --setObjectExtTable(self.node:getChildByTag(1020), self)
  self.hechengButton = TypeCast(self.node:getChildByTag(1020), "CCButton")
  setObjectExtTable(self.hechengButton, self)
  self.hechengButton:setEnabled(false)
  --TypeCast(self.node:getChildByTag(1020), "CCButton"):setEnabled(false)
  --local button = self.node:getChildByTag(ZBHCLayer.NodeID.QiangHuaButton)
  --setObjectExtTable(button, self);
  --self:createBagTable()
  --self:createHeroEquipLayer()
  --self:setMenuFocus(0)
  self.bagOrHero = 0
  self.radioMenu1:setMenuState(0)
  self.radioMenu2:setMenuState(1)
end

ZBHCLayerFunc.createBagTable = function(self)
  local tb = self.node:getChildByTag(ZBHCLayer.NodeID.TableBK)
  return BagTable:create(TypeCast(tb, "CCSprite"), self, "onClickEquip", 1) 
  --self.node:addChild(self.bagTable.node)
end

ZBHCLayerFunc.createHeroEquipLayer = function(self)
  self.heroEquip = HeroEquipLayer:create(self, "onClickEquip")
  return self.heroEquip
  --self.node:addChild(self.heroEquipLayer.node)
end

ZBHCLayerFunc.clearEquipInfo = function(self)
  self.node:removeChild(self.leftSp, true)
  self.node:removeChild(self.topSp, true)
  self.node:removeChild(self.rightSp, true)
end

ZBHCLayerFunc.setEquipInfo = function(self, equipIndex)
  self:clearEquipInfo()
  
  local equip = Equip:create(equipIndex) 
  local sp  = addSpriteFromBK(self.node, 2001, nameFromPath(equip.equipConfig.asset))
  self.leftSp = sp
  self.currentEquipIndex = equipIndex 
  self.currentEquip = equip
 
  local itemLevel = 1 
  if self.upType==0 then
    itemLevel = math.floor((equip.equipConfig.level+10)/10)

    local equipConfig = UserData.config.models.Equip[equip.equipConfig.upLevelId]
    sp = addSpriteFromBK(self.node, 2003, nameFromPath(equipConfig.asset)) 
    self.topSp = sp
    --self.sprites[#self.sprites+1] = sp
    TypeCast(self.node:getChildByTag(1010), "CCLabelTTF"):setString(string.format("%d%%", equip.equipConfig.levelRate*100)) 
    TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(string.format("%d现金", equip.equipConfig.upLevelCash)) 
  else
    itemLevel = Equip.QualityConfig[equip.equipConfig.quality].id + 10
      
    local equipConfig = UserData.config.models.Equip[equip.equipConfig.upQualityId]
    sp = addSpriteFromBK(self.node, 2003, nameFromPath(equipConfig.asset)) 
    self.topSp = sp
    --self.sprites[#self.sprites+1] = sp
    TypeCast(self.node:getChildByTag(1010), "CCLabelTTF"):setString(string.format("%d%%", equip.equipConfig.qualityRate*100)) 
    TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(string.format("%d现金", equip.equipConfig.upQualityCash)) 
  end
  local tmpItem = Item:createWithLevel(itemLevel)
  local itemLayer = tmpItem:createItemLayer("onClickRight", self, 1)
  itemLayer:setPosition(self.node:getChildByTag(2002):getPosition())
  self.node:addChild(itemLayer)
  self.rightSp = itemLayer
  self.currentStone = tmpItem 
  if tmpItem.itemInfo~=nil and tmpItem.itemInfo.n>=1 then
    TypeCast(self.node:getChildByTag(1020), "CCButton"):setEnabled(true)
  else
    TypeCast(self.node:getChildByTag(1020), "CCButton"):setEnabled(false)
  end
end
ZBHCLayerFunc.onClickRight = function(self, node, event)
end
ZBHCLayerFunc.showShineAction = function(self, bksp)
  CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFramesWithFile("shine.plist")
  local sp = CCSprite:createWithSpriteFrameName("shine0001.png")
  sp:setPosition(bksp:getPosition())
  self.node:addChild(sp)
  local ac = ActionUtil.createAction("shine.plist",0.02,"")
  sp:runAction(ActionUtil.createSeqAction({ac, CCCallFuncN:create(ZBHCLayerFunc.onShineEnd)}))
end

ZBHCLayerFunc.onShineEnd = function(node)
  print("ZBHCLayerFunc.onShineEnd", node)
  ZBHCLayer.instance:clearEquipInfo()
  node:getParent():removeChild(node, true)
  setTouchEnabledAll(ZhuangBeiScene.instance.node, true)
  ZBHCLayer.instance.hechengButton:setEnabled(false)
 
  if ZBHCLayer.instance.bagOrHero==1 then
    local tb = ZBHCLayer.instance.node:getChildByTag(1003)
    ZBHCLayer.instance.node:removeChild(ZBHCLayer.instance.contentLayer, true)
    ZBHCLayer.instance.contentLayer = BagTable:create(TypeCast(tb, "CCSprite"), ZBHCLayer.instance, "onClickBagEquip", 1).node 
    ZBHCLayer.instance.node:addChild(ZBHCLayer.instance.contentLayer)
  else
    ZBHCLayer.instance.heroEquip:showEquip()
  end
end

ZBHCLayerFunc.onHeChengButton = function(self, node, event)
  if self.currentEquipIndex==nil then
    showMessage("没有选择装备", ccc3(255,0,0))
    return
  end
  print("ZBHCLayerFunc.onHeChengButton", self.upType, self.currentStone.itemLevel.id, self.currentEquipIndex)
  if self.upType==0 then 
    NetworkRequest.upLevelEquip(self.currentStone.itemLevel.id, self.currentEquipIndex, "onHeChengComplete", self)
  else
    NetworkRequest.upQualityEquip(self.currentStone.itemLevel.id, self.currentEquipIndex, "onHeChengComplete", self)
  end
  LoadScene:showLoadingScene()
  --setTouchEnabledAll(ZhuangBeiScene.instance.node, false)
  --self.hechengButton:setEnabled(false)
  --self:showShineAction(self.topSp)
end
ZBHCLayerFunc.onHeChengComplete = function(self, data)
  print("ZBHCLayerFunc.onHeChengButton", data)
  LoadScene:removeLoadingScene()
  --self.hechengButton:setEnabled(true)
  self.currentEquipIndex = nil
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    self:showShineAction(self.topSp)
  else
    showMessage(res.msg, ccc3(255,255,0))
    setTouchEnabledAll(ZhuangBeiScene.instance.node, true)
    ZBHCLayer.instance.hechengButton:setEnabled(false)
  end
end
ZBHCLayerFunc.onClickEquip = function(self, node, event)
  print(self, node, node:getTag())
  if node:getTag()<100000 then
    self:setEquipInfo(node:getTag())
  end
end

ZBHCLayerFunc.menuItem1Changed = function(self, oldIndex, newIndex)
  --self.radioMenu1:setMenuState(newIndex)
  self.node:removeChild(self.contentLayer, true)
  self.bagOrHero = newIndex
  if newIndex==0 then
    self.contentLayer = self:createHeroEquipLayer().node
  else
    self.contentLayer = self:createBagTable().node
  end
  self.node:addChild(self.contentLayer)
  --self.currentSelect = index
end

ZBHCLayerFunc.menuItem2Changed = function(self, oldIndex, newIndex)
  --self.radioMenu2:setMenuState(newIndex)
  self.upType = newIndex
  if self.currentEquipIndex~=nil then
    self:setEquipInfo(self.currentEquipIndex)
  end
end
